<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { useUserInfoStore } from '@/stores/userInfo';
import { ElMessage, ElMessageBox } from 'element-plus';
import {
  CaretBottom,
  Goods,
  Management,
  UserFilled,
  User,
  EditPen,
  SwitchButton,
  Promotion
} from '@element-plus/icons-vue';

const userInfoStore = useUserInfoStore();
const router = useRouter();

// 获取用户信息
const getUserInfo = async () => {
  try {
    await userInfoStore.fetchUserInfo();
    if (!userInfoStore.isLoggedIn) {
      await router.push('/login');
    }
  } catch (error) {
    console.error('获取用户信息失败:', error);
    await router.push('/login');
  }
};

// 页面加载时获取用户信息
onMounted(getUserInfo);

// 处理菜单命令
const handleCommand = (command) => {
  if (command === 'logout') {
    ElMessageBox.confirm('你确认要退出吗？', '温馨提示', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      userInfoStore.logout(); // 调用 logout 方法
      ElMessage.success('退出成功');
      router.push('/login'); // 重定向到登录页面
    });
  } else {
    router.push(`/user/${command}`);
  }
};
</script>

<template>
  <el-container class="layout-container" v-if="userInfoStore.isLoggedIn">
    <!-- 左侧菜单 -->
    <el-aside width="200px">
      <div class="logo"></div>
      <el-menu
          router
          active-text-color="#ffd04b"
          background-color="#232323"
          text-color="#fff"
      >
        <!-- 普通用户菜单 -->
        <template v-if="(!userInfoStore.isAdmin)">
          <el-menu-item index="/user/shopping"> <!-- 改成我们的购物广场 -->
            <el-icon><Goods /></el-icon>
            <span>购物广场</span>
          </el-menu-item>

          <el-menu-item index="/user/orders">
            <el-icon><Promotion /></el-icon>
            <span>我的订单</span>
          </el-menu-item>

          <el-menu-item index="/review/reviews">
            <el-icon><Management /></el-icon>
            <span>我的评价</span>
          </el-menu-item>

          <el-menu-item index="/user/info">
            <el-icon><User /></el-icon>
            <span>基本资料</span>
          </el-menu-item>

          <el-menu-item index="/user/resetPassword">
            <el-icon><EditPen /></el-icon>
            <span>重置密码</span>
          </el-menu-item>
        </template>

        <!-- 管理员菜单 -->
        <template v-else>
          <el-sub-menu index="/admin">
            <template #title>
              <el-icon><Management /></el-icon>
              <span>系统管理</span>
            </template>
            <el-menu-item index="/admin/products">商品管理</el-menu-item>
            <el-menu-item index="/admin/orders">订单管理</el-menu-item>
            <el-menu-item index="/admin/reviews">评论管理</el-menu-item>
            <el-menu-item index="/admin/users">用户管理</el-menu-item>
          </el-sub-menu>
        </template>
      </el-menu>
    </el-aside>

    <!-- 右侧主区域 -->
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <div class="welcome">欢迎，{{ userInfoStore.userInfo.username }}</div>
        <el-dropdown placement="bottom-end" @command="handleCommand">
          <span class="dropdown-trigger">
            <el-avatar :src="userInfoStore.userInfo.avatar || '@/assets/default.png'" />
            <el-icon><CaretBottom /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="info">基本资料</el-dropdown-item>
              <el-dropdown-item command="resetPassword">重置密码</el-dropdown-item>
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>

      <!-- 中间内容区域 -->
      <el-main>
        <router-view />
      </el-main>

      <!-- 底部区域 -->
      <el-footer>后台管理系统 ©2024</el-footer>
    </el-container>
  </el-container>

  <div v-else class="loading-container">
    <el-skeleton animated />
  </div>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  .el-aside {
    background-color: #232323;
    .logo {
      height: 60px;
      background-color: #333;
      display: flex;
      align-items: center;
      justify-content: center;
      color: white;
      font-weight: bold;
    }
    .el-menu {
      border-right: none;
    }
  }
  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    .welcome {
      font-size: 16px;
    }
  }
}

.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
</style>